package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.Sensitive;
import com.heima.wemedia.mapper.SensitiveMapper;
import com.heima.wemedia.service.SensitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 嘉德罗斯
 * @date 2021/11/29
 */
@Service
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper,Sensitive> implements SensitiveService {

    @Autowired
    private SensitiveMapper sensitiveMapper;

    @Override
    public ResponseResult list(SensitiveDto dto) {
        LambdaQueryWrapper <Sensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(dto.getName()!=null,Sensitive::getSensitives,dto.getName());
        queryWrapper.orderByDesc(Sensitive::getCreatedTime);
        IPage<Sensitive> page= new Page<>(dto.getPage(), dto.getSize());
        IPage<Sensitive> sensitiveIPage = sensitiveMapper.selectPage(page, queryWrapper);
        List<Sensitive> records = sensitiveIPage.getRecords();
        return ResponseResult.okResult(records);
    }

    @Override
    public ResponseResult update(SensitiveDto dto) {
        Sensitive sensitive = new Sensitive();
        sensitive.setId(dto.getId());
        sensitive.setSensitives(dto.getSensitives());
        sensitiveMapper.updateById(sensitive);
        return ResponseResult.okResult("修改成功");
    }

    @Override
    public ResponseResult save(String sensitives) {
        Sensitive sensitive = new Sensitive();
        sensitive.setSensitives(sensitives);
        sensitive.setCreatedTime(new Date());
        sensitiveMapper.insert(sensitive);
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult del(Integer id) {
        sensitiveMapper.deleteById(id);
        return ResponseResult.okResult("删除成功");
    }
}
